package p0test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class LuceneTest {

    @Test
    public void test1() {

        IndexWriter indexWriter = null;
        try {
            //1,创建directory （索引放在内存中还是磁盘上）
            //Directory directory=new RAMDirectory();
            Directory directory = FSDirectory.open(new File("d://lucene/index01"));
            //2,创建indexwriter （写索引器）
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
            indexWriter = new IndexWriter(directory, iwc);
            Document doc = null;
            //3 创建document
            File file = new File("d:/p0test");
            for (File f : file.listFiles()) {
                System.out.println(f.getName());
                doc = new Document();
                doc.add(new Field("content", new FileReader(f)));
                //后两个参数不是是否把这个文件名存储到硬盘中，和 是否要对这个文件名进行分词
                doc.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                doc.add(new Field("path", f.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                //4,通过indexwriter 添加索引
                indexWriter.addDocument(doc);
            }

            System.out.println("------index is over----");
        } catch (CorruptIndexException e) {
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (indexWriter != null) {
                try {
                    indexWriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void test2() {
        try {
            //1，创建directory
            Directory directory = FSDirectory.open(new File("d://lucene/index01"));
            //2,创建indexreader
            IndexReader reader=IndexReader.open(directory);
            //3,根据indexreader创建indexsearch
            IndexSearcher isearch=new IndexSearcher(reader);
            //4,创建搜索query
            QueryParser parser=new QueryParser(Version.LUCENE_35,"content",new StandardAnalyzer(Version.LUCENE_35));
            //表示搜索内容中包含java的
            Query query=parser.parse("book");
            //5,根据search搜索并且返回 topDoc
            //第二个参数表示搜索多少条
            TopDocs tdc=isearch.search(query,2);
            //6,根据topDoc 对象获取ScoreDoc对象
            ScoreDoc [] sdcs=tdc.scoreDocs;
            for (ScoreDoc sdc:sdcs) {
                //7 根据search 和scoreDocD对象获取具体的document对象
                Document document=isearch.doc(sdc.doc);
                //8 根据doc对象获取需要的值
                System.out.println(document.get("filename"));
            }

            reader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
